core.bdshare = {
	status : false,
	//给工厂调用的接口
	_init : function(){
		core.bdshare.init(attrs);
		return true;
	},
	init:function(callback){
		//加载百度分享代码
		if(!core.bdshare.status) {
			$.ajax({type:'GET',url:'http://bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5),dataType: 'script',ifModified:true,cache:true,success:function(){core.bdshare.status=true;callback&&callback();}});
		}else{
			callback && callback();
			window._bd_share_main.init();
		}
	},

	addConfig: function(itemName, config){
		if(!core.bdshare.status) {
			core.bdshare.init(function(){
				core.bdshare._addConfig(itemName, config);
			});
		}else{
			core.bdshare._addConfig(itemName, config);
		}
	},

	_addConfig : function(itemName, config){
		if (!window._bd_share_config) {
			window._bd_share_config = new Array();
		}
		if (!itemName || itemName == 'common') {
			window._bd_share_config["common"] = config;
		} else {
			if(!window._bd_share_config[itemName]) {
				window._bd_share_config[itemName] = [];
			}
			var i = window._bd_share_config[itemName].length;
			window._bd_share_config[itemName][i] = config;
		}
		if(core.bdshare.status) {
			window._bd_share_main.init();
		}
	},

	feedlistConfig: false,
	feedlist: function (obj){
		if(!core.bdshare.feedlistConfig){
			core.bdshare.addConfig('share', {
				"tag" : "share_feedlist",
				"onBeforeClick":function(cmd, config){
					if(window.event.target){
						var target = window.event.target;
					}else{
						var target = window.event.srcElement;
					}
					var dd = $(target).parents('dd');
					var content = dd.find('.contents:first p').clone();
					content.find('dl').remove();
					content = $.trim(content.text());
					var url = dd.find('p.info a.date').attr('href');
					config["bdUrl"]  = url;
					config["bdText"] = content;
					config["bdDesc"] = content;
					config["bdPic"]  = '';
					return config;
				}
			});
			core.bdshare.feedlistConfig = true;
		}
		var dd   = $(obj).parents('dd');
		var line = dd.find('.infopen:first');
		var box  = dd.find('.forward_box:first');
		var cmt  = $(obj.parentModel.childModels['comment_detail'][0]);
		if(cmt.size()) cmt.hide();
		if(box.is(':hidden')){
			line.show().find('.trigon').css('left',
			  $(obj).position().left+ ($(obj).width()/2));
			box.stop().slideDown(200);//.show();
			/*var s = box.find('.share_feedlist');
			s.css('top', (-s.outerHeight(true))+'px');
			s.stop().animate({top:0}, 200);*/
		}else{
			box.stop().slideUp(200, function(){
				line.hide();
			});
		}
	},

    sharequestionConfig: false,
    sharequestion: function (obj){
        if(!core.bdshare.sharequestionConfig){
            core.bdshare.addConfig('share', {
                "tag" : "share_question",
                "onBeforeClick":function(cmd, config){
                    if(window.event.target){
                        var target = window.event.target;
                    }else{
                        var target = window.event.srcElement;
                    }
                    var div = $(target).closest('.send-question-box');
                    var title = $.trim(div.find('h1').text());
                    var content = div.find('p').text();
                    var bdPic = '';
                    if (div.find('img').length > 0) {
                        bdPic = div.find('img')[0].src;
                    }
                    var url = dd.find('p.info a.date').attr('href');
                    config["bdUrl"]  = url;
                    config["bdText"] = title;
                    config["bdDesc"] = '';
                    config["bdPic"] = bdPic;
                    config['bdComment'] = content;
                    return config;
                }
            });
            core.bdshare.sharequestionConfig = true;
        }
        //console.log($(obj).html());
        var dd   = $(obj).closest('.send-question-box');
        var line = dd.find('.infopen:first');
        var box  = dd.find('.forward_box:first');
        var cmt  = $(obj.parentModel.childModels['comment_detail'][0]);
        if(cmt.size()) cmt.hide();
        if(box.is(':hidden')){
            line.show().find('.trigon').css('left',
                $(obj).position().left+ ($(obj).width()/2));
            box.stop().slideDown(200);
        }else{
            box.stop().slideUp(200, function(){
                line.hide();
            });
        }
    },
    shareAnswerConfig: false,
    shareAnswer: function (obj){
        if(!core.bdshare.sharequestionConfig){
            core.bdshare.addConfig('share', {
                "tag" : "share_answer",
                "onBeforeClick":function(cmd, config){
                    if(window.event.target){
                        var target = window.event.target;
                    }else{
                        var target = window.event.srcElement;
                    }
                    // var div = $(target).closest('.send-question-box');
                    var title = $.trim($('h1').text());

                    var dd = $(target).closest('dd');
                    var content = $.trim(dd.find('.post-content').text());
                    var bdPic = '';
                    if (dd.find('img').length > 0) {
                        bdPic = dd.find('img')[0].src;
                    }
                    var url = dd.find('p.info a.date').attr('href');
                    config["bdUrl"]  = url;
                    config["bdText"] = title;
                    config["bdDesc"] = '';
                    config["bdPic"] = bdPic;
                    config['bdComment'] = content;
                    return config;
                }
            });
            core.bdshare.shareAnswerConfig = true;
        }
        var dd   = $(obj).parents('dd');
        var line = dd.find('.infopen:first');
        var box  = dd.find('.forward_box:first');
        var cmt  = dd.find('comment_detail');//$(obj.parentModel.childModels['comment_detail'][0]);
        if(cmt.size()) cmt.hide();
        if(box.is(':hidden')){
            line.show().find('.trigon').css('left',
                $(obj).position().left+ ($(obj).width()/2));
            box.stop().slideDown(200);
        }else{
            box.stop().slideUp(200, function(){
                line.hide();
            });
        }
    }
};
